﻿Public Class CategorieAjout

    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")

    Dim position As Integer

    Private Sub CategorieAjout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.Left = 0
        Me.Top = 0

        Connection()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAjout.Click


        If BtnAjout.Text = "Ajouter" Then
            TxtIdCat.Text = macnx.Generer_Cle_Primaire("numeroCategorie", "categoriemateriel")
            TxtNomCat.Text = ""

            MessageBox.Show("Attention ! , Pour ajouter une categorie veuillez remplir tous les champs !", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            BtnAjout.Text = "Valider!"
        Else
            Dim reponse = MessageBox.Show("Vous êtes sur le point d'ajouter un nouveau matériel, en êtes vous bien sur ...?!", "Insertion", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If reponse = vbOK Then
                If TxtNomCat.Text <> "" Then
                    Dim a As Integer
                    Dim b As String

                    a = CInt(TxtIdCat.Text)
                    b = TxtNomCat.Text

                    macnx.Executer_Requete_Action("INSERT INTO categoriemateriel VALUES ( " & a & ", '" & b & "' )")

                    position = 0
                    RechargerLesDonnees()
                    Rafraichir()

                    BtnAjout.Text = "Ajouter"
                Else
                    MessageBox.Show("Erreur de saisie...!", "   ALERTE!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                BtnAjout.Text = "Ajouter"
            End If
        End If
    End Sub







    Private Sub BtnModif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnModif.Click
        If BtnModif.Text = "Modifier" Then
            TxtIdCat.Text = ""
            TxtNomCat.Text = ""

            MessageBox.Show("Attention ! ,Veuillez selectioner la ligne et modifier le champ  pour modifier !", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            BtnModif.Text = "Valider!"
        Else
            If TxtIdCat.Text <> "" Then

                Dim reponse = MessageBox.Show("La categorie  va être MODIFIER.., Confirmez-vous?!", "MODIFICATION", MessageBoxButtons.YesNo, MessageBoxIcon.Information)

                If MessageBox.Show("La categorie  va être MODIFIER.., Confirmez-vous?!", "MODIFICATION", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then

                    macnx.Se_Connecter_MySqlServer()
                    macnx.Executer_Requete_Action(" UPDATE categoriemateriel Set nomcategorie='" & TxtNomCat.Text & "'  where numerocategorie = " & TxtIdCat.Text & "")
                    macnx.Se_Deconnecter_MySqlServer()

                    Rafraichir()

                    TxtIdCat.Text = ""
                    TxtNomCat.Text = ""

                    BtnModif.Text = "Modifier"

                Else
                    TxtIdCat.Text = ""
                    TxtNomCat.Text = ""

                    BtnModif.Text = "Modifier"

                End If
            End If
        End If
    End Sub

    Private Sub DgCategorie_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgCategorie.CellClick
        If DgCategorie.CurrentCell.Value Is DBNull.Value Then
            TxtIdCat.Enabled = False

        Else

            TxtIdCat.Text = DgCategorie.CurrentRow.Cells(0).Value()
            TxtNomCat.Text = DgCategorie.CurrentRow.Cells(1).Value()
        End If
    End Sub




    Private Sub BtnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSupprimer.Click

        Dim position As Integer


        If BtnSupprimer.Text = "Supprimer" Then


            TxtNomCat.Text = ""


            MessageBox.Show("Attention ! , Pour supprimer un matériel veuillez sélectionnez la ligne correspondant à celui-ci !", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            BtnSupprimer.Text = "Valider!"

        Else
            If TxtIdCat.Text <> "" Then
                Dim nu As Integer
                nu = TxtIdCat.Text

                Dim reponse = MessageBox.Show("Le matériel va être SUPPRIMER, confirmez vous cet suppression...?!", "SUPPRESSION", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

                If reponse = vbOK Then

                    macnx.Se_Connecter_MySqlServer()
                    macnx.Executer_Requete_Action("Delete from categoriemateriel Where numerocategorie= " & nu & "")
                    macnx.Se_Deconnecter_MySqlServer()
                    position = 0

                    Rafraichir()

                    BtnAjout.Text = "Supprimer"
                    TxtIdCat.Text = ""
                    TxtNomCat.Text = ""

                Else
                    TxtIdCat.Text = ""
                    TxtNomCat.Text = ""

                    BtnSupprimer.Text = "Supprimer"
                End If

            End If
        End If
    End Sub

    Private Sub RechargerLesDonnees()

        Connection()
    End Sub

    Public Sub Connection()
        macnx.Se_Connecter_MySqlServer()
        DgCategorie.DataSource = Nothing
        macnx.viderVue("les_Categories")
        DgCategorie.DataSource = macnx.Executer_Requete_Select("SELECT * FROM categoriemateriel ", "les_Categories")
        macnx.Se_Deconnecter_MySqlServer()
    End Sub

    Public Sub Rafraichir()

        TxtIdCat.Text = DgCategorie.Rows(0).Cells(0).Value.ToString
        'TxtIdCat.Text = Connection.MonJeuDeDonnees.Tables("Toutcategorie").Rows(position).Item(0)
        'TxtNomCat.Text = Connection.MonJeuDeDonnees.Tables("Toutcategorie").Rows(position).Item(1)
        ''TxtPrix.Text = connection.MonJeuDeDonnees.Tables("Toutmateriel").Rows(position).Item(2)
        ''txtquantiter.Text = connection.MonJeuDeDonnees.Tables("Toutmateriel").Rows(position).Item(3)
        ''txtreference2.Text = connection.MonJeuDeDonnees.Tables("Toutmateriel").Rows(position).Item(4)
        'DgCategorie.DataSource = Connection.MonJeuDeDonnees.Tables("Toutcategorie")


    End Sub
End Class